package com.yunfangdata.service.impl;

import com.yunfangdata.domain.Permission;
import com.yunfangdata.mapper.PermissionMapper;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IPermissionService;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private RequestMappingHandlerMapping rmhm;

    @Autowired
    private PermissionMapper mapper;

    public void save(Permission entity) {
        mapper.insert(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void update(Permission entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public Permission get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);

        if (total == 0){
            return new PageResult();
        }
        List<Permission> rows = mapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    //权限加载
    public void load() {

        //排除重复
        List<Permission> permissions = mapper.selectAll();
        Set<String> resources = new HashSet<>();
        for (Permission permission : permissions) {
            resources.add(permission.getResource());
        }

        //通过请求映射处理映射器对象 获取该对象所有的方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();

        //遍历方法对象判断方法上 是否拥有requiresPermission 注解
        for (HandlerMethod hm : values) {
            RequiresPermissions permissionResource = hm.getMethodAnnotation(RequiresPermissions.class);
            PermissionName permissionName = hm.getMethodAnnotation(PermissionName.class);
            if (permissionResource == null || permissionName==null) {
                continue;
            }
            //获取controller 方法权限
            String resource = permissionResource.value()[0];
            String name = permissionName.value();
            if (resources.contains(resource)){
                continue;
            }
            //保存权限
            Permission permission = new Permission();
            permission.setName(name);
            permission.setResource(resource);
            mapper.insert(permission);

        }

    }

    public List<Permission>  permissions() {
        return mapper.permissions();
    }

    public List<Permission> permissionsByRoleId(Long id) {
        return mapper.permissionsByRoleId(id);
    }

    public List<String> queryResourceByEmployeeId(Long id) {
        return mapper.queryResourceByEmployeeId(id);
    }

}
